
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[gspSiteUserExtended_SEARCH]
(
@userId int = null,
@firstName nvarchar(50) = null,
@middleName nvarchar(50) = null,
@lastName nvarchar(50) = null,
@jobId int = null,
@jobPosition nvarchar(200) = null,
@birthDate datetime = null,
@phone1 nvarchar(100) = null,
@phone2 nvarchar(100) = null,
@icq nvarchar(15) = null,
@skype nvarchar(100) = null
)
AS

SELECT
  *
FROM
  [dbo].[SiteUserExtended]
INNER JOIN SiteUser 
	ON (SiteUserExtended.UserId = SiteUser.UserId)
WHERE
  (@userId IS NULL OR SiteUserExtended.[UserId] = @userId)
AND
  (@firstName IS NULL OR @firstName = '' OR [FirstName] LIKE @firstName + '%')
AND
  (@middleName IS NULL OR @middleName = '' OR [MiddleName] LIKE @middleName + '%')
AND
  (@lastName IS NULL OR @lastName = '' OR [LastName] LIKE @lastName + '%')
AND
  (@jobId IS NULL OR [JobId] = @jobId)
AND
  (@jobPosition IS NULL OR @jobPosition = '' OR [JobPosition] LIKE @jobPosition + '%')
AND
  (@birthDate IS NULL OR [BirthDate] = @birthDate)
AND
  (@phone1 IS NULL OR @phone1 = '' OR [Phone1] LIKE @phone1 + '%')
AND
  (@phone2 IS NULL OR @phone2 = '' OR [Phone2] LIKE @phone2 + '%')
AND
  (@icq IS NULL OR @icq = '' OR [ICQ] LIKE @icq + '%')
AND
  (@skype IS NULL OR @skype = '' OR [Skype] LIKE @skype + '%')
AND 
   SiteUser.IsDeleted = 0
GO
